package com.kly.msg.strategy;

import com.kly.enums.ProviderEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName: StrategyHelper
 * @description: 策略helper实现类
 * @author: yuwn
 * @create: 2023-11-16 16:47
 */
@Component
public class StrategyHelper {

    private Map<String,SmsStrategy> strategyMap = new ConcurrentHashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    private void init(){
        strategyMap.putAll(applicationContext.getBeansOfType(SmsStrategy.class));
    }

    public SmsStrategy getStrategy(Integer code){
        String providerName = ProviderEnum.getByCode(code).getDesc();
        return this.getStrategyByName(providerName);
    }

    private SmsStrategy getStrategyByName(String name){
        if(StringUtils.isNotEmpty(name)){
            return  strategyMap.get(name);
        }
        return null;
    }
}
